package com.ce.pub.upload.logic.search;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.ce.framework.datamaintain.action.SearchAction;
import com.ce.framework.datamaintain.logic.SQLSearchCondition;
import com.ce.framework.exception.AppException;
import com.ce.framework.util.StringUtil;
import com.ce.pub.upload.view.PubFileUploadLogView;
import com.ce.sys.login.model.User;

public class UploadSearchCondition  extends SQLSearchCondition {

	/** 类型，0为显示历史上传文件，1为显示本人收藏夹 */
	private int tp;//类型，0为显示历史上传文件，1为显示本人收藏夹
	private String fileType;//类别，上传时指定的模块名称，如gdjh等,vc_child$folder中第一个/之前的内容
	private String vcUserId;
	private String vcUserName;
	private String vcFileName;
	private String beginDate;
	private String endDate;
	
	public void initCondition(SearchAction action,boolean search) throws AppException {
		if(search){
			User user = User.getUser();
			vcUserId = user.getVcId();
			vcUserName = user.getVcName();
		}
	}

	public String getSearchSql() throws AppException {
		StringBuffer sql = new StringBuffer("select t.vc_id,t.vc_filename,t.dt_add$time,t.n_filesize,t.vc_user$name from pub_t_file_upload_log t where 1=1 ");
		if(tp==0){
			sql.append("and t.vc_user$id='").append(vcUserId).append("' ");
		}
		else if(tp==1){
			sql.append("and exists (select 1 from pub_t_user_favorite t2 where t2.vc_user$id='")
			.append(vcUserId).append("' and t.vc_id=t2.vc_favorite) ");
		}
		if(StringUtil.isNotBlank(fileType)){
			sql.append("and t.vc_child$folder like '%'||${fileType}||'%' ");
		}
		if(StringUtil.isNotBlank(vcFileName)){
			sql.append("and t.vc_filename like '%'||${vcFileName}||'%' ");
		}
		sql.append(this.getDateCondition("t.dt_add$time", beginDate, endDate));
		sql.append("order by t.dt_add$time desc");
		return sql.toString();
	}

	@Override
	protected String getOrderBy() {
		if("vcOrgName".equals(sort)){
			return "t2.vc_name "+order;
		}
		return super.getOrderBy();
	}

	public Object fromSQLQuery(ResultSet rs, int num) {
		PubFileUploadLogView view = new PubFileUploadLogView();
		try {
			view.setVcId(rs.getString("VC_ID"));
			view.setVcFilename(rs.getString("VC_FILENAME"));
			view.setDtAddTime(rs.getTimestamp("DT_ADD$TIME"));
			view.setNfilesize(rs.getLong("N_FILESIZE"));
			view.setVcUserName(rs.getString("VC_USER$NAME"));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return view;
	}

	public String getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(String beginDate) {
		this.beginDate = beginDate;
	}

	public String getEndDate() {
		return endDate;
	}

	public void setEndDate(String endDate) {
		this.endDate = endDate;
	}

	public String getVcFileName() {
		return vcFileName;
	}

	public void setVcFileName(String vcFileName) {
		this.vcFileName = vcFileName;
	}

	public String getVcUserId() {
		return vcUserId;
	}

	public void setVcUserId(String vcUserId) {
		this.vcUserId = vcUserId;
	}

	public String getVcUserName() {
		return vcUserName;
	}

	public void setVcUserName(String vcUserName) {
		this.vcUserName = vcUserName;
	}

	public int getTp() {
		return tp;
	}

	public void setTp(int tp) {
		this.tp = tp;
	}

	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

}
